using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fyp.Models;
using WebMatrix.WebData;
using System.Web.Security;
namespace MvcApplication.Controllers
{
    public class SubmitDailyTaskController : Controller
    {
        FYPEntities5 db = new FYPEntities5();
      //
        // GET: /SubmitDailyTask/


        public ActionResult StudentView()
        {
            var userName = WebSecurity.CurrentUserName;
        
            if ((global.indtasklst.Count() == 0) && (global.grptasklst.Count() == 0))
            {
                int sid = 0, gid = 0;
                var query = from regId in db.Students
                            where userName == regId.Std_Email
                           
                            select regId;
                foreach (var value in query)
                {
                    sid = value.SId;
                    gid = Convert.ToInt32(value.GId);
                }
                var qInd = from IndtId in db.IndividualTasks
                           where IndtId.SId == sid
                           select IndtId;
                foreach (var value in qInd)
                {
                    if (value.Status != "Submitted")
                    {
                        IndividualTask it = new IndividualTask();
                        it.TId = value.TId;
                        it.Description = value.Description;
                        it.AdvisorName = value.AdvisorName;
                        it.Assigned_Date = value.Assigned_Date;
                        it.Assigned_Time = value.Assigned_Time;
                        it.Assigned_Task_Path = value.Assigned_Task_Path;
                        it.SId = value.SId;
                        it.Status = value.Status;
                        global.indtasklst.Add(it);
                    }
                }
                var qGrp = from GrptId in db.GroupTasks
                           where GrptId.GId == gid
                           select GrptId;
                foreach (var value in qGrp)
                {
                    if (value.Status != "Submitted")
                    {
                        GroupTask gt = new GroupTask();
                        gt.TId = value.TId;
                        gt.Description = value.Description;
                        gt.AdvisorName = value.AdvisorName;
                        gt.Date = value.Date;
                        gt.Time = value.Time;
                        gt.Path = value.Path;
                        gt.GId = value.GId;
                        gt.Status = value.Status;
                        global.grptasklst.Add(gt);
                    }
                }
            }
            return View();
        }
        public ActionResult SubmitTask(FormCollection fc, HttpPostedFileBase Attach)
        {
            string path = null;
            int j = 0;
            string s = "c";
            string v = null;
            string comnt = "comnt";
            string cn = null;
            int totalsize = global.grptasklst.Count() + global.indtasklst.Count();
            int indsize = global.indtasklst.Count();
            IndividualTask idt = new IndividualTask();
            if (fc["DropDownList1"] == "Assigned")
            {
                for (int i = 0; i < totalsize; i++)
                {
                    v = @s + i;
                    cn = @comnt + i;
                    if (i < indsize)
                    {
                        if (global.indtasklst.Count() != 0)
                        {
                            idt.TId = global.indtasklst[i].TId;
                            idt.SId = global.indtasklst[i].SId;
                            idt.Description = global.indtasklst[i].Description;
                            idt.AdvisorName = global.indtasklst[i].AdvisorName;
                            idt.Assigned_Date = global.indtasklst[i].Assigned_Date;
                            idt.Assigned_Time = global.indtasklst[i].Assigned_Time;
                            idt.Assigned_Task_Path = global.indtasklst[i].Assigned_Task_Path;
                            idt.SId = global.indtasklst[i].SId;
                            if (Request.Form[@v] == "on")
                            {
                                global.indtasklst.RemoveAt(i);
                                for (int k = 0; k < Request.Files.Count; k++)
                                {
                                    if (Request.Files[i] != null)
                                    {
                                        if (k == i)
                                        {
                                            path = System.IO.Path.Combine(Server.MapPath("~/Data/"), Request.Files[i].FileName);
                                            if (Request.Files[i].FileName != "")
                                            {
                                                idt.Submitted_Task_Path = path;
                                                Request.Files[i].SaveAs(path);
                                            }
                                            k = Request.Files.Count;
                                        }
                                    }
                                }
                                DateTime now = DateTime.Now;
                                idt.Submission_Date = now.GetDateTimeFormats('d')[0];
                                idt.Submission_Time = now.GetDateTimeFormats('t')[0];
                                if (fc[cn] != null)
                                    idt.Comments = fc[cn];
                                idt.Status = "Submitted";
                                IndividualTask getrecord = db.IndividualTasks.SingleOrDefault(t => t.TId == idt.TId);
                                db.Entry(getrecord).CurrentValues.SetValues(idt);
                                db.SaveChanges();
                            }
                        }
                    }
                    else
                    {
                        var userName = WebSecurity.CurrentUserName;
                        if(global.grptasklst.Count()!=0)
                        {
                        GroupTask grt = new GroupTask();
                        grt.TId = global.grptasklst[j].TId;
                        grt.Description = global.grptasklst[j].Description;
                        grt.AdvisorName = global.grptasklst[j].AdvisorName;
                        grt.Date = global.grptasklst[j].Date;
                        grt.Time = global.grptasklst[j].Time;
                        grt.Path = global.grptasklst[j].Path;
                        grt.GId = global.grptasklst[j].GId;
                        grt.Status = global.grptasklst[j].Status;
                        var query = from regId in db.Students
                                    where regId.Std_Email == userName
                                    select regId;
                        foreach (var value in query)
                        {
                            grt.SId = value.SId;
                        }
                        if (Request.Form[@v] == "on")
                        {
                            global.grptasklst.RemoveAt(j);
                            for (int k = 0; k < Request.Files.Count; k++)
                            {
                                if (Request.Files[i] != null)
                                {
                                    if (k == i)
                                    {
                                        path = System.IO.Path.Combine(Server.MapPath("~/Data/"), Request.Files[i].FileName);
                                        if (Request.Files[i].FileName != "")
                                        {
                                            grt.Submitted_task_path = path;
                                            Request.Files[i].SaveAs(path);
                                        }
                                        k = Request.Files.Count;
                                    }
                                }
                            }
                            DateTime now = DateTime.Now;
                            grt.Submission_Date = now.GetDateTimeFormats('d')[0];
                            grt.Submission_Time = now.GetDateTimeFormats('t')[0];
                            if (fc[cn] != null)
                                grt.Comments = fc[cn];
                            grt.Status = "Submitted";
                            GroupTask getrecord = db.GroupTasks.SingleOrDefault(t => t.TId == grt.TId);
                            db.Entry(getrecord).CurrentValues.SetValues(grt);
                            db.SaveChanges();
                            global.grptasklst.Remove(grt);
                        }
                        }
                    }
                }
            }
            else if (fc["DropDownList1"] == "By Self")
            {
                var userName = WebSecurity.CurrentUserName;
        
                IndividualTask id = new IndividualTask();
                path = "";
                int sid = 0;
                var query = from regId in db.Students
                            where regId.Std_Email== userName
        
                            select regId;
                foreach (var value in query)
                {
                    sid = value.SId;
                }
                id.SId = sid;
                id.AdvisorName = "Self";
                id.Status = "Submitted";
                DateTime nw = DateTime.Now;
                id.Assigned_Date = nw.GetDateTimeFormats('d')[0];
                id.Assigned_Time = nw.GetDateTimeFormats('t')[0];
                id.Description = fc["tdescription"];
                id.Comments = fc["tcomment"];
                nw = DateTime.Now;
                id.Submission_Date = nw.GetDateTimeFormats('d')[0];
                id.Submission_Time = nw.GetDateTimeFormats('t')[0];
                if (Attach != null)
                {
                    string fname = Attach.FileName;
                    path = System.IO.Path.Combine(Server.MapPath("~/Data/"), Attach.FileName);
                    Attach.SaveAs(path);
                }
                id.Submitted_Task_Path = path;
                id.Assigned_Task_Path = path;
                db.IndividualTasks.Add(id);
                db.SaveChanges();
            }
            return View("StudentView");
        }
        public ActionResult TeacherView()
        {
            var query = from grups in db.Groups
                        select grups;
            foreach (var value in query)
            {
                Group g = new Group();
                g.GId = value.GId;
                global.glst.Add(g);
            }
            return View();
        }
        public ActionResult ShowStudents(FormCollection p)
        {
            int gid = Convert.ToInt32(p["DropDownList1"]);
            var query = from stu in db.Students
                        where stu.GId == gid
                        select stu;
            foreach (var value in query)
            {
                Student ss = new Student();
                ss.SId = value.SId;
                ss.Std_Name = value.Std_Name;
                ss.Std_RegId = value.Std_RegId;
                ss.Degree = value.Degree;
                ss.Std_Email = value.Std_Email;
                global.lst.Add(ss);
            }
            global.groupid = gid;
            global.grptasklst.Clear();
            global.indtasklst.Clear();
            return View("TeacherView");
        }

        public ActionResult Assign(FormCollection fc, HttpPostedFileBase upload)
        {
            var userName = WebSecurity.CurrentUserName;
        
            string path = null;
            string cc1 = Request.Form["all"];
            if (cc1 == "on")
            {
                // ViewBag.Message = "dakha on";
                GroupTask gt = new GroupTask();
                Project p = new Project();

                var getAdvisorName = (from pro in db.Projects
                        where userName == pro.AdvisorEmail
                        select pro.AdvisorName).FirstOrDefault();
              
                DateTime now = DateTime.Now;
                gt.Date = now.GetDateTimeFormats('d')[0];
                gt.Time = now.GetDateTimeFormats('t')[0];
                gt.Description = fc["tdescription"];
                gt.AdvisorName = getAdvisorName;
                if (upload != null)
                {
                    string fname = upload.FileName;
                    path = System.IO.Path.Combine(Server.MapPath("~/Data/"), upload.FileName);
                    upload.SaveAs(path);
                }
                gt.Path = path;
                gt.GId = global.groupid;
                gt.Status = "Assigned";
                db.GroupTasks.Add(gt);
                db.SaveChanges();
            }
            else
            {
                //ViewBag.Message = "dakha off";
                int objcount = 0, i = 0;
                int[] arr = new int[5];
                string c1 = Request.Form["c1"];
                string c2 = Request.Form["c2"];
                string c3 = Request.Form["c3"];
                string c4 = Request.Form["c4"];
                string c5 = Request.Form["c5"];
                if (c1 == "on")
                {
                    objcount++;
                    i = i + 1;
                    arr[i] = global.lst[0].SId;
                }
                if (c2 == "on")
                {
                    objcount++;
                    i = i + 1;
                    arr[i] = global.lst[1].SId;
                }
                if (c3 == "on")
                {
                    objcount++;
                    i = i + 1;
                    arr[i] = global.lst[2].SId;
                }
                if (c4 == "on")
                {
                    objcount++;
                    i = i + 1;
                    arr[i] = global.lst[3].SId;
                }
                if (c5 == "on")
                {
                    objcount++;
                    i = i + 1;
                    arr[i] = global.lst[4].SId;
                }
               
                var getAdvisorName = (from pro in db.Projects
                                      where userName == pro.AdvisorEmail
                                      select pro.AdvisorName).FirstOrDefault();

               


                if (upload != null)
                {
                    string fname = upload.FileName;
                    path = System.IO.Path.Combine(Server.MapPath("~/Data/"), upload.FileName);
                    upload.SaveAs(path);
                }
                for (int j = 1; j <= objcount; j++)
                {
                    IndividualTask sit = new IndividualTask();
                    sit.AdvisorName = getAdvisorName;
                    sit.Status = "Assigned";
                    DateTime now = DateTime.Now;
                    sit.Assigned_Date = now.GetDateTimeFormats('d')[0];
                    sit.Assigned_Time = now.GetDateTimeFormats('t')[0];
                    sit.Description = fc["tdescription"];
                    sit.Assigned_Task_Path = path;
                    sit.SId = arr[j];
                    db.IndividualTasks.Add(sit);
                    db.SaveChanges();
                }
            }
            global.lst.Clear();
            return View("TeacherView");
        }
        public ActionResult ViewTaskList()
        {
            global.grptasklst.Clear();
            global.indtasklst.Clear();
            return View();
        }
        //public FileResult DownloadDoc(int dID)
        //{
        //    string filePath = getFilePath(dID);
        //    string contentType = getContentType(dID);
        //    filePath = Path.Combine(Server.MapPath(dr.GetString("FilePath")), dr.GetString("FileName"));
        //    return File(filePath, contentType);
        //}
        public ActionResult ShowTasks(FormCollection fc)
        {
            var getStudentEmail = WebSecurity.CurrentUserName;

            int sid = 0, gid = 0;
            var query = from regId in db.Students
                        where getStudentEmail == regId.Std_Email
                        select regId;
            foreach (var value in query)
            {
                sid = value.SId;
                gid = Convert.ToInt32(value.GId);
            }
            if (fc["DropDownList1"] == "Assigned")
            {
                global.grptasklst.Clear();
                global.indtasklst.Clear();
                var qInd = from IndtId in db.IndividualTasks
                           where IndtId.SId == sid
                           select IndtId;
                foreach (var value in qInd)
                {
                    if ((value.Status != "Submitted") && (value.AdvisorName == "Self"))
                    {
                        IndividualTask it = new IndividualTask();
                        it.TId = value.TId;
                        it.Description = value.Description;
                        it.AdvisorName = value.AdvisorName;
                        it.Assigned_Date = value.Assigned_Date;
                        it.Assigned_Time = value.Assigned_Time;
                        it.Assigned_Task_Path = value.Assigned_Task_Path;
                        it.SId = value.SId;
                        it.Status = value.Status;
                        global.indtasklst.Add(it);
                    }
                }
                var qGrp = from GrptId in db.GroupTasks
                           where GrptId.GId == gid
                           select GrptId;
                foreach (var value in qGrp)
                {
                    if (value.Status != "Submitted")
                    {
                        GroupTask gt = new GroupTask();
                        gt.TId = value.TId;
                        gt.Description = value.Description;
                        gt.AdvisorName = value.AdvisorName;
                        gt.Date = value.Date;
                        gt.Time = value.Time;
                        gt.Path = value.Path;
                        gt.GId = value.GId;
                        gt.Status = value.Status;
                        global.grptasklst.Add(gt);
                    }
                }
                //ViewBag.indassgnedcount = global.indtasklst.Count();
                //ViewBag.grpassgnedcount = global.grptasklst.Count();
            }
            else if (fc["DropDownList1"] == "Submitted")
            {
                global.grptasklst.Clear();
                global.indtasklst.Clear();
                var qInd = from IndtId in db.IndividualTasks
                           where IndtId.SId == sid
                           select IndtId;
                foreach (var value in qInd)
                {
                    if ((value.Status == "Submitted") && (value.AdvisorName == "Self"))
                    {
                        IndividualTask it = new IndividualTask();
                        it.TId = value.TId;
                        it.Description = value.Description;
                        it.AdvisorName = value.AdvisorName;
                        it.Assigned_Date = value.Assigned_Date;
                        it.Assigned_Time = value.Assigned_Time;
                        it.Assigned_Task_Path = value.Assigned_Task_Path;
                        it.SId = value.SId;
                        it.Status = value.Status;
                        global.indtasklst.Add(it);
                    }
                }
                var qGrp = from GrptId in db.GroupTasks
                           where GrptId.GId == gid
                           select GrptId;
                foreach (var value in qGrp)
                {
                    if (value.Status == "Submitted")
                    {
                        GroupTask gt = new GroupTask();
                        gt.TId = value.TId;
                        gt.Description = value.Description;
                        gt.AdvisorName = value.AdvisorName;
                        gt.Date = value.Date;
                        gt.Time = value.Time;
                        gt.Path = value.Path;
                        gt.GId = value.GId;
                        gt.Status = value.Status;
                        global.grptasklst.Add(gt);
                    }
                }
                //ViewBag.indsubcount = global.indtasklst.Count();
                //ViewBag.grpsubcount = global.grptasklst.Count();
            }
            return View("ViewListTask");
        }
    }
}
